package com.qinghe.pro.tag;

import java.io.IOException;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.SimpleTagSupport;

import com.qinghe.utils.Page;

public class PaginationFormTag extends SimpleTagSupport {

	private Page page;
	private String action;
	private String onsubmit;

	@Override
	public void doTag() throws JspException, IOException {
		StringBuilder builder = new StringBuilder();
		if (onsubmit != null) {
			builder.append("<form id=\"p_pagerForm\" method=\"post\" action=\"" + action + "\" onsubmit=\"" + onsubmit + "\">\n");
		} else {
			builder.append("<form id=\"p_pagerForm\" method=\"post\" action=\"" + action + "\">\n");
		}

		builder.append("<input type=\"hidden\" id=\"p_pageNum\" name=\"pageNum\" value=\"" + page.getPageNum() + "\"/>\n");
		builder.append("<input type=\"hidden\" id=\"p_numPerPage\" name=\"numPerPage\" value=\"" + page.getNumPerPage() + "\"/>\n");
		builder.append("<input type=\"hidden\" name=\"orderField\" value=\"" + page.getOrderField() + "\"/>\n");
		builder.append("<input type=\"hidden\" name=\"orderDirection\" value=\"" + page.getOrderDirection() + "\"/>\n");
		builder.append("<input type=\"hidden\" name=\"totalCount\" value=\"" + page.getTotalCount() + "\"/>\n");

		getJspContext().getOut().write(builder.toString());

		getJspBody().invoke(null);

		getJspContext().getOut().write("</form>\n");
	}

	public void setPage(Page page) {
		this.page = page;
	}

	public void setAction(String action) {
		this.action = action;
	}

	public void setOnsubmit(String onsubmit) {
		this.onsubmit = onsubmit;
	}

}
